package fun.chutianshu.eescore_full.controller;

import fun.chutianshu.eescore_full.entity.Department;
import fun.chutianshu.eescore_full.entity.Profession;
import fun.chutianshu.eescore_full.service.ProfessionService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 专业表(Profession)表控制层
 *
 * @author makejava
 * @since 2021-04-23 16:10:23
 */
@RestController
@RequestMapping("profession")
public class ProfessionController {
    /**
     * 服务对象
     */
    @Resource
    private ProfessionService professionService;

    /**
     * 通过主键查询单条数据，仅获取专业信息
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Profession selectOne(Integer id) {
        return this.professionService.queryById(id);
    }

    /**
     * 通过主键查询单条数据，仅获取所在系部信息
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectDeptByProfession/{id}")
    public Department selectDeptByProfession(@PathVariable(name="id") Integer id) {
        return this.professionService.queryDepartmentById(id);
    }

    /**
     * 通过主键查询单条数据，获取专业和所在系部信息
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectProAndDeptByProId/{id}")
    public Profession selectProAndDeptByProId(@PathVariable(name="id") Integer id) {
        return this.professionService.queryProAndDeptByProId(id);
    }

    /**
     * 通过主键查询单条数据，仅获取所在系部信息
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectDeptByProfession1/{id}")
    public Department selectDeptByProfession1(@PathVariable(name="id") Integer id) {
        return this.professionService.queryProAndDeptByProId(id).getDepartment();
    }

    @RequestMapping("/userinfo")
    @ResponseBody
    public String productInfo(){
        String currentUser = "";
        Object principl = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principl instanceof UserDetails) {
            currentUser = ((UserDetails)principl).getUsername();
        }else {
            currentUser = principl.toString();
        }
        return " 当前用户是: "+currentUser;
    }
}
